/**
 * 
 */
package cn.softTank.BpelModelImpl;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import cn.softTank.BpelModel.Activity;
import cn.softTank.BpelModel.CorrelationSets;
import cn.softTank.BpelModel.EventHandler;
import cn.softTank.BpelModel.Extensions;
import cn.softTank.BpelModel.FaultHandler;
import cn.softTank.BpelModel.Import;
import cn.softTank.BpelModel.MessageExchanges;
import cn.softTank.BpelModel.Namespace;
import cn.softTank.BpelModel.PartnerLinks;
import cn.softTank.BpelModel.Process;
import cn.softTank.BpelModel.Variables;

/**
 * @author Yuzone
 * 
 */
public class ProcessImpl extends ExtensibleElementImpl implements Process {

	private String name;
	private String targetNamespace;
	private String queryLanguage;
	private String expressionLanguage;
	private boolean exitOnStandardFault;
	private boolean suppressJoinFailure;
	private boolean setExitOnStandardFault = false;
	private boolean setSuppressJoinFailure = false;
	private Activity activity;
	private List<Import> imports;
	private MessageExchanges messageExchanges;
	private PartnerLinks partnerLinks;
	private Variables variables;
	private List<Namespace> namespace;

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getAbstractProcessProfile()
	 */
	@Override
	public String getAbstractProcessProfile() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getActivity()
	 */
	@Override
	public Activity getActivity() {
		return activity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getCorrelationSets()
	 */
	@Override
	public CorrelationSets getCorrelationSets() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getEventHandlers()
	 */
	@Override
	public EventHandler getEventHandlers() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getExitOnStandardFault()
	 */
	@Override
	public boolean getExitOnStandardFault() {
		return this.exitOnStandardFault;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getExpressionLanguage()
	 */
	@Override
	public String getExpressionLanguage() {
		return this.expressionLanguage;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getExtensions()
	 */
	@Override
	public Extensions getExtensions() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getFaultHandlers()
	 */
	@Override
	public FaultHandler getFaultHandlers() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getImports()
	 */
	@Override
	public List<Import> getImports() {
		return imports;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getMessageExchanges()
	 */
	@Override
	public MessageExchanges getMessageExchanges() {
		return messageExchanges;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getName()
	 */
	@Override
	public String getName() {
		return this.name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getPartnerLinks()
	 */
	@Override
	public PartnerLinks getPartnerLinks() {
		return partnerLinks;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getQueryLanguage()
	 */
	@Override
	public String getQueryLanguage() {
		return this.queryLanguage;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getSuppressJoinFailure()
	 */
	@Override
	public Boolean getSuppressJoinFailure() {
		return this.suppressJoinFailure;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getTargetNamespace()
	 */
	@Override
	public String getTargetNamespace() {
		return this.targetNamespace;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#getVariables()
	 */
	@Override
	public Variables getVariables() {
		return variables;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#isSetAbstractProcessProfile()
	 */
	@Override
	public boolean isSetAbstractProcessProfile() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#isSetExitOnStandardFault()
	 */
	@Override
	public boolean isSetExitOnStandardFault() {
		return this.setExitOnStandardFault;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#isSetSuppressJoinFailure()
	 */
	@Override
	public boolean isSetSuppressJoinFailure() {
		return this.setSuppressJoinFailure;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setAbstractProcessProfile(java.lang.String)
	 */
	@Override
	public void setAbstractProcessProfile(String value) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setActivity(cn.softTank.BpelModel.Activity)
	 */
	@Override
	public void setActivity(Activity value) {
		activity = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setCorrelationSets(cn.softTank.BpelModel
	 * .CorrelationSets)
	 */
	@Override
	public void setCorrelationSets(CorrelationSets value) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setEventHandlers(cn.softTank.BpelModel.
	 * EventHandler)
	 */
	@Override
	public void setEventHandlers(EventHandler value) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#setExitOnStandardFault(boolean)
	 */
	@Override
	public void setExitOnStandardFault(boolean value) {
		this.exitOnStandardFault = value;
		this.setExitOnStandardFault = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setExpressionLanguage(java.lang.String)
	 */
	@Override
	public void setExpressionLanguage(String value) {
		this.expressionLanguage = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setExtensions(cn.softTank.BpelModel.Extensions
	 * )
	 */
	@Override
	public void setExtensions(Extensions value) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setFaultHandlers(cn.softTank.BpelModel.
	 * FaultHandler)
	 */
	@Override
	public void setFaultHandlers(FaultHandler value) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#setImports(java.util.List)
	 */
	@Override
	public void setImports(List<Import> value) {
		imports = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#addImports(cn.softTank.BpelModel.Import)
	 */
	@Override
	public void addImports(Import value) {
		if (imports == null)
			imports = new ArrayList<Import>();
		imports.add(value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#removeImportAt(int)
	 */
	@Override
	public void removeImportAt(int index) {
		if (imports != null && index < imports.size())
			imports.remove(index);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setMessageExchanges(cn.softTank.BpelModel
	 * .MessageExchanges)
	 */
	@Override
	public void setMessageExchanges(MessageExchanges value) {
		messageExchanges = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#setName(java.lang.String)
	 */
	@Override
	public void setName(String value) {
		this.name = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecn.softTank.BpelModel.Process#setPartnerLinks(cn.softTank.BpelModel.
	 * PartnerLinks)
	 */
	@Override
	public void setPartnerLinks(PartnerLinks value) {
		partnerLinks = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#setQueryLanguage(java.lang.String)
	 */
	@Override
	public void setQueryLanguage(String value) {
		this.queryLanguage = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setSuppressJoinFailure(java.lang.Boolean)
	 */
	@Override
	public void setSuppressJoinFailure(Boolean value) {
		this.suppressJoinFailure = value;
		this.setSuppressJoinFailure = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#setTargetNamespace(java.lang.String)
	 */
	@Override
	public void setTargetNamespace(String value) {
		this.targetNamespace = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.softTank.BpelModel.Process#setVariables(cn.softTank.BpelModel.Variables
	 * )
	 */
	@Override
	public void setVariables(Variables value) {
		variables = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#unsetAbstractProcessProfile()
	 */
	@Override
	public void unsetAbstractProcessProfile() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#unsetExitOnStandardFault()
	 */
	@Override
	public void unsetExitOnStandardFault() {
		this.setExitOnStandardFault = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.softTank.BpelModel.Process#unsetSuppressJoinFailure()
	 */
	@Override
	public void unsetSuppressJoinFailure() {
		this.setSuppressJoinFailure = false;
	}

	@Override
	public void setNamespace(List<Namespace> value) {
		namespace = value;
	}

	@Override
	public List<Namespace> getNamespace() {
		return namespace;
	}

	@Override
	public void addNamespace(Namespace value) {
		if (namespace == null)
			namespace = new ArrayList<Namespace>();
		namespace.add(value);
	}

	public Element toElement() {
		super.toElement();
		node.setName("process");
		if (name != null)
			node.addAttribute("name", name);
		if (targetNamespace != null)
			node.addAttribute("targetNamespace", targetNamespace);
		if (queryLanguage != null)
			node.addAttribute("queryLanguage", queryLanguage);
		if (expressionLanguage != null)
			node.addAttribute("expressionLanguage", expressionLanguage);
		if (setSuppressJoinFailure == true)
			if (suppressJoinFailure)
				node.addAttribute("suppressJoinFailure", "yes");
			else
				node.addAttribute("suppressJoinFailure", "no");
		if (setExitOnStandardFault == true)
			if (exitOnStandardFault)
				node.addAttribute("exitOnStandardFault", "yes");
			else
				node.addAttribute("exitOnStandardFault", "no");
		node.addNamespace("",
				"http://docs.oasis-open.org/wsbpel/2.0/process/executable");
		if (namespace != null)
			for (int m = 0; m < namespace.size(); m++)
				node.addNamespace(namespace.get(m).getPrefix(), namespace
						.get(m).getAddress());
		if (imports != null)
			for (int n = 0; n < imports.size(); n++)
				node.add(imports.get(n).toElement());
		if (partnerLinks != null)
			node.add(partnerLinks.toElement());
		if (messageExchanges != null)
			node.add(messageExchanges.toElement());
		if (variables != null)
			node.add(variables.toElement());
		if (activity != null)
			node.add(activity.toElement());
		return node;
	}

	public String toXml() {
		String xml = "<process";
		if (name != null)
			xml += " name=" + name;
		if (targetNamespace != null)
			xml += " targetNamespace=" + targetNamespace;
		if (queryLanguage != null)
			xml += " queryLanguage=" + queryLanguage;
		if (expressionLanguage != null)
			xml += " expressionLanguage=" + expressionLanguage;
		if (setSuppressJoinFailure == true)
			if (suppressJoinFailure)
				xml += " suppressJoinFailure=yes";
			else
				xml += " suppressJoinFailure=no";
		if (setExitOnStandardFault == true)
			if (exitOnStandardFault)
				xml += " exitOnStandardFault=yes";
			else
				xml += " exitOnStandardFault=no";
		xml += " xmlns=http://docs.oasis-open.org/wsbpel/2.0/process/executable>";
		xml += "</process>";
		return xml;
	}
}
